from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from student.models import  Students, Courses, SelectClass, Comments
from django.http import JsonResponse

# from django.contrib.auth.decorators import login_required


def show_courses(request):
    course_list = Courses.objects.all()
    return render(request, 'student/show_courses.html', {'course_list': course_list})

def show_selectedcourse(request):
    user, is_create = Students.objects.get_or_create(user=request.user)
    # studentid = user.username
    qs = SelectClass.objects.values()
    select_list = qs.filter(studentid=user)
    result_list = []
    for selected in select_list:
        tmp = {}
        courses = Courses.objects.get(id=selected["courseid_id"])
        courseid = courses.courseid
        coursename = courses.coursename
        tmp["courseid"] = courseid
        tmp["coursename"] = coursename

        studentid = user.username
        tmp["studentid"] = studentid
        result_list.append(tmp)

    return render(request, 'student/show_select.html', {'result_list': result_list})
    

def register(request):
    try:
        username = request.POST.get("username")
        name = request.POST.get("name")
        password = request.POST.get("password")


        user = User(username=username, last_name=name)
        user.set_password(password)
        user.save()
        Students.objects.create(user=user,username=username,student_name=name)
        
    except Exception as err:
        result = False
        message = str(err)
    else:
        result = True
        message = "Register success!"

    return JsonResponse({"result": result, "message": message})

def show_addcourse_page(request):
    return render(request, "student/add_course.html")


def add_course(request):
    try:
        courseid = request.GET.get("courseid")
        coursename = request.GET.get("coursename")
        teacher = request.GET.get("teacher")
        schooltime = request.GET.get("schooltime")
        place = request.GET.get("place")
        grade = request.GET.get("grade")
        Courses.objects.create(
                                courseid=courseid,
                                coursename=coursename,
                                teacher=teacher,
                                schooltime=schooltime,
                                place=place,
                                grades=grade
                                )
 
    except Exception as err:
        result = False
        message = str(err)
    else:
        result = True
        message = "课程添加成功!"

    return JsonResponse({"result": result, "message": message})

def select_course(request):
    """
        操作选课数据库,首先获取当前登录用户的username，作为主键
        再拿到course,courseid,comment
        插入到数据库中
    """
    # username = request.user.username
    try:
        user, is_created = Students.objects.get_or_create(user=request.user)
        courseid = request.GET.get("courseid")
        coursename = request.GET.get("coursename")
        print(coursename)
        course = Courses.objects.get(courseid=courseid)
        
        record = SelectClass.objects.create(studentid=user,
                                    courseid=course,
                                    coursename=coursename)
        print(courseid)
        record.save()
    except Exception as err:
        result = False
        message = str(err)
    else:
        result = True
        message = "选课成功!"

    return JsonResponse({"result": result, "message": message})



def show_user(request):
    user = Students.objects.get(user=request.user)

    user_info = Students.objects.get(username=user.username)
    
    lastname = user_info.student_name
    username = user_info.username
    print(lastname)
    print(username)
    return render(request, 'student/show_student.html', {'lastname' : lastname, 'username' : username})



def show_comments(request):
    courseid = request.GET.get('courseid')
    """
        拿到courseid参数，再去查询数据库，拿到所有的和该门课程相关的所有评论
    """
    
    course_obj = Courses.objects.get(courseid=courseid)

    qs = SelectClass.objects.filter(courseid__id=course_obj.id)    

    comment_list = []
    for comment in qs:
        tmp = []
        print("comment.studentid_id",comment.studentid_id)
        stu_obj = Students.objects.get(id=comment.studentid_id)
        print(stu_obj.student_name)
        print(stu_obj.username)
        print(comment.comment)
        tmp.append(stu_obj.username)
        tmp.append(stu_obj.student_name)
        tmp.append(course_obj.coursename)
        com_obj = Comments.objects.filter(studentid_id=comment.studentid_id,coursename=course_obj.coursename)
        for com in com_obj:
            print("com_obj.comment",com.comment)
            tmp.append(com.comment)
        comment_list.append(tmp)

    return render(request, 'student/show_comment.html', {"comment_list":comment_list})


def add_comment(request):
    try:
        user = Students.objects.get(user=request.user)
        studentid = request.GET.get("studentid")
        coursename = request.GET.get("coursename")
        comment = request.GET.get("comment")
        print(coursename)

        record = Comments.objects.create(
                                    studentid=user,
                                    coursename=coursename,comment=comment)
        record.save()
    except Exception as err:
        result = False
        message = str(err)
    else:
        result = True
        message = "评论发表成功!"

    return JsonResponse({"result": result, "message": message})
